package com.pzj.swtcallcom.ocx.ie;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.internal.ole.win32.COM;
import org.eclipse.swt.internal.ole.win32.GUID;
import org.eclipse.swt.internal.ole.win32.IUnknown;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

public class AutomationClientSite extends OleClientSite {
	public AutomationClientSite(Composite parent, int style, String progId) {
		super(parent, style);
		try {
			appClsid = getClassID(progId);
			if (appClsid == null)
				OLE.error(OLE.ERROR_INVALID_CLASSID);
			int[] address = new int[1];
			int result = COM.CoCreateInstance(getClassID(progId), 0,
					COM.CLSCTX_INPROC_SERVER | COM.CLSCTX_LOCAL_SERVER,
					COM.IIDIUnknown, address);
			if (result != COM.S_OK)
				OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);
			objIUnknown = new IUnknown(address[0]);
		} catch (SWTException e) {
			dispose();
			disposeCOMInterfaces();
			throw e;
		}
	}

	protected GUID getClassID(String progId) {
		GUID guid = new GUID();
		char[] buffer = null;
		if (progId != null) {
			int count = progId.length();
			buffer = new char[count + 1];
			progId.getChars(0, count, buffer, 0);
		}
		if (COM.CLSIDFromProgID(buffer, guid) != COM.S_OK) {
			int result = COM.CLSIDFromString(buffer, guid);
			if (result != COM.S_OK)
				OLE.error(result);
		}
		return guid;
	}

	public static void main(String[] args) {
		Shell composite = new Shell();
		OleFrame frame = new OleFrame(composite, SWT.NONE);
		AutomationClientSite client = new AutomationClientSite(frame, SWT.NONE,
				"InternetExplorer.Application");
		OleAutomation ole = new OleAutomation(client);
		int[] ids = ole.getIDsOfNames(new String[] { "visible" });
		ole.setProperty(ids[0], new Variant(true));
	}
}
